package com.dm.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dm.shop.common.enums.ResultEnum;
import com.dm.shop.entity.OrderDetail;
import com.dm.shop.entity.OrderList;
import com.dm.shop.mapper.OrderDetailMapper;
import com.dm.shop.mapper.OrderListMapper;
import com.dm.shop.service.OrderListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@Service
public class OrderListServiceImpl extends ServiceImpl<OrderListMapper, OrderList> implements OrderListService {

    @Autowired
    private OrderListMapper orderListMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public Result getOrderListInfoByUid(int uid) {
        List<Map> orderListInfo = orderListMapper.getOrderListInfoByUid(uid);
        if(orderListInfo != null)
        {
            return ResultUtil.success(orderListInfo);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getOrderInfoPage(int uid, int page, int size) {
        List<Map> orderListInfo = orderListMapper.getOrderListInfoByUid(uid);
        List<Map> res = new ArrayList<>();
        //获取当前一页的订单信息
        for(int i = (page - 1) * size; i < page * size ; i++)
        {
            //通过下标获取 订单记录
            Map temp = orderListInfo.get(i);
            //添加属性 当前页数 大小  订单量
            temp.put("page",page);
            temp.put("size",size);
            temp.put("total",orderListInfo.size());
            res.add(temp);
        }
        if(res.size() != 0)
        {
            return ResultUtil.success(res);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllOrderInfo(int page, int size) {
        List<Map> allOrderInfo = orderListMapper.getAllOrderInfo();
        List<Map> res = new ArrayList<>();
        for(int i = (page - 1) * size; i < page * size && i < allOrderInfo.size(); i++)
        {
            //通过下标获取 订单记录
            Map temp = allOrderInfo.get(i);
            //添加属性 当前页数 大小  订单量
            temp.put("page",page);
            temp.put("size",size);
            temp.put("total",allOrderInfo.size());
            res.add(temp);
        }
        if(res.size() != 0)
        {
            return ResultUtil.success(res);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result delOrder(int id) {

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oid",id);
        Integer isDelDetail = orderDetailMapper.delete(queryWrapper);
        boolean IsDel = this.removeById(id);
        if(IsDel != true)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        else
        {
            if (isDelDetail != 0)
            {
                return ResultUtil.success(ResultEnum.SUCCESS);
            }
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result addOrder(OrderList orderList) {
        boolean IsSave = this.save(orderList);
        if(IsSave != true)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        else
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    @Override
    public Result editOrder(OrderList orderList) {
        boolean IsUpdate = this.updateById(orderList);
        if(IsUpdate != true)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        else
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    @Override
    public Result getProductInfoById(int id) {
        List<Map> productInfo = orderListMapper.getProductInfoById(id);
        if(productInfo != null)
        {
            return ResultUtil.success(productInfo);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result updateStatusById(int id) {
        OrderList orderList = this.getById(id);
        //修改订单状态为已审核
        orderList.setStatus("已审核");
        boolean IsUpdate = this.updateById(orderList);
        if(IsUpdate != true)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        else
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }
}
